package com.example.chenhong

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.chenhong.model.Student

class AddStudentActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_student)
        val confirmButton = findViewById<Button>(R.id.confirmButton)
        val idEditText = findViewById<TextView>(R.id.idEditText)
        val nameEditText = findViewById<TextView>(R.id.nameEditText)
        val genderEditText = findViewById<TextView>(R.id.genderEditText)
        val classNameEditText = findViewById<TextView>(R.id.classNameEditText)
        val cancelButton = findViewById<Button>(R.id.cancelButton)

        confirmButton.setOnClickListener {
            val id = idEditText.text.toString()
            val name = nameEditText.text.toString()
            val gender = genderEditText.text.toString()
            val className = classNameEditText.text.toString()

            val newStudent = Student(id, name, gender, className)
            val intent = Intent().apply {
                putExtra("student", newStudent)
            }
            setResult(RESULT_OK, intent)
            finish()
        }

        cancelButton.setOnClickListener {
            finish()
        }
    }
}